<?php

namespace App\Listeners;

use App\Events\MoneyChangeEvent;
use App\Models\Fund;
use App\Models\MoneyLog;
use App\Models\Stock;
use App\Models\User;
use App\Models\UserLevel;
use Carbon\Carbon;

class MoneyChangeListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  MoneyChangeEvent $event
     * @return void
     */
    public function handle(MoneyChangeEvent $event)
    {
        //
        $order_no = $event->order_no;
        $type = $event->type;
        if ($event->user_id > 0) {
            $user = User::whereId($event->user_id)->first();
            $buy_user = User::whereId($event->buy_user)->first();
            $level = UserLevel::whereId($user->level)->first();
            $target_id = $event->user_id;
            if ($buy_user->level > 1) {
                $target_id = $buy_user->id;
            }
            $parent_id = User::whereId($target_id)->first()->parent;

            $pay_date = Carbon::createFromDate(date('Y'), date('m'), config('pay_month_day'))->hour(0)->minute(0)->second(0);
            $pay_date->addMonth(1);

            if ($type == 'fund') {

                $fund = Fund::whereOrderNo($order_no)->first();
                $pvData = [6000, 10000, 12000];
                if ($fund && $fund->status) {
                    $log = MoneyLog::whereOrderNo($order_no)->where('type', 'fund')->first();
                    if (empty($log)) {
                        $money = $fund->num * $pvData[$fund->year - 1];
                        $insert['name'] = '基金奖金分成';
                        $insert['money'] = $money * $level->fund_ratio * 0.01;
                        $insert['type'] = 'fund';
                        $insert['change_type'] = true;
                        $insert['pay_status'] = 0;
                        $insert['pay_at'] = $pay_date;
                        $insert['order_no'] = $order_no;
                        $insert['user_id'] = $target_id;
                        $insert['buy_user'] = $event->buy_user;
                        $insert['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
//                        dd($insert);
                        $log = MoneyLog::create($insert);
//                        $this->updateUser($log);

                        $this->createRadio($target_id, $parent_id, $event->order_no, $money, $event->buy_user, "fund");
//                        $this->checkRadio($user, $event, 'fund');
                    }
                }
            } else if ($type == 'stock') {
                $stock = Stock::whereOrderNo($order_no)->first();
                if ($stock && $stock->status) {
                    $log = MoneyLog::whereOrderNo($order_no)->where('type', 'stock')->first();
                    if (empty($log)) {
                        $insert['name'] = '股权奖金分成';
                        $insert['money'] = $stock->num * $stock->price * $level->stock_ratio * 0.01;
                        $insert['type'] = 'stock';
                        $insert['change_type'] = true;
                        $insert['pay_status'] = 1;
                        $insert['pay_at'] = Carbon::now();
                        $insert['order_no'] = $order_no;
                        $insert['user_id'] = $target_id;
                        $insert['buy_user'] = $event->buy_user;
                        $insert['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
                        $log = MoneyLog::create($insert);
                        $this->updateUser($log);
                        $this->createRadio($target_id, $parent_id, $event->order_no, $stock->num * $stock->price, $event->buy_user, "stock");


                    }
                }
            }
        }
    }

    public function createRadio($user_id, $parent_id, $order_no, $money, $order_creator, $type = "fund")
    {
        $current_user = User::whereId($user_id)->first();
        $pay_date = Carbon::createFromDate(date('Y'), date('m'), config('pay_month_day'))->hour(0)->minute(0)->second(0);
        $pay_date->addMonth(1);
        if ($parent_id && $parent_id > 0) {
            $parent_user = User::whereId($parent_id)->first();
            if ($parent_user->level - $current_user->level > 0) {

                if ($type == "fund") {
                    $name = '差额奖金分成-基金';
                    $ratio = $parent_user->levelDetail->fund_ratio - $current_user->levelDetail->fund_ratio;
                } else {
                    $name = '差额奖金分成-股权';
                    $ratio = $parent_user->levelDetail->stock_ratio - $current_user->levelDetail->stock_ratio;
                }

                $insert['name'] = $name;
                $insert['money'] = $money * $ratio * 0.01;
                $insert['type'] = 'radio';
                $insert['change_type'] = true;
                $insert['order_no'] = $order_no;
                $insert['user_id'] = $parent_user->id;
                $insert['pay_status'] = 0;
                $insert['pay_at'] = $pay_date;
                $insert['buy_user'] = $order_creator;
                $insert['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
                $log = MoneyLog::create($insert);
//                $this->updateUser($log);
            }

            if ($parent_user->level > 3 && $parent_user->level < 6 && $current_user->level == $parent_user->level) {
                $insert['name'] = "同阶奖金分成";
                $insert['money'] = $money * 0.5 * 0.01;
                $insert['type'] = 'same';
                $insert['change_type'] = true;
                $insert['order_no'] = $order_no;
                $insert['user_id'] = $parent_id;
                $insert['pay_at'] =
                $insert['pay_status'] = 0;
                $insert['buy_user'] = $order_creator;
                $insert['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
                $log = MoneyLog::create($insert);
//                $this->updateUser($log);
            }
            $this->createRadio($parent_id, $parent_user->parent, $order_no, $money, $order_creator, $type);
        }
    }

//    public function checkRadio(User $user, MoneyChangeEvent $event, $type = 'fund')
//    {
//        $buy_user_id = $event->buy_user;
//        $buy_user = User::whereId($buy_user_id)->first();
//        $money = $event->money;
//        if ($user->level - $buy_user->level > 0 && $buy_user->level > 1) {
//            if ($type == 'fund') {
//                $name = '差额奖金分成-基金';
//                $pvData = [6000, 10000, 12000];
//                $order_no = $event->order_no;
//
//                $fund = Fund::whereOrderNo($order_no)->first();
//
//                $money = $pvData[$fund->year];
//                $ratio = $user->levelDetail->fund_ratio - $buy_user->levelDetail->fund_ratio;
//            } else {
//                $name = '差额奖金分成-股权';
//                $ratio = $user->levelDetail->stock_ratio - $buy_user->levelDetail->stock_ratio;
//                $money = $event->money;
//            }
//
//            $insert['name'] = $name;
//            $insert['money'] = $money * $ratio * 0.01;
//            $insert['type'] = 'radio';
//            $insert['change_type'] = true;
//
//            $insert['order_no'] = $event->order_no;
//            $insert['user_id'] = $user->id;
//            $insert['buy_user'] = $event->buy_user;
//            $insert['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
//            $log = MoneyLog::create($insert);
//            $this->updateUser($log);
//        }
//        if ($user->level > 3 && $user->level < 6 && $buy_user->level == $user->level) {
//            $insert['name'] = "同阶奖金分成";
//            $insert['money'] = $event->money * 0.5 * 0.01;
//            $insert['type'] = 'same';
//            $insert['change_type'] = true;
//            $insert['order_no'] = $event->order_no;
//            $insert['user_id'] = $user->id;
//            $insert['buy_user'] = $event->buy_user;
//            $insert['money_no'] = "M" . date('YmdHis') . rand(1000, 9999);
//            $log = MoneyLog::create($insert);
//            $this->updateUser($log);
//        }
//    }

    private function updateUser(MoneyLog $log)
    {
        $user = $log->user;
        $user->money += $log->money;
        $user->save();
    }
}
